package com.graphics;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Test_1 extends  JFrame {
    private MyPanel mp=null;
    public static void main(String[] args) {
        new Test_1();
    }
    public Test_1(){
        mp=new MyPanel();
        this.add(mp);
        this.addKeyListener(mp);
        this.setSize(1080,680);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}


class MyPanel extends JPanel implements KeyListener {
    private ZD zd=null;
    public MyPanel(){
        zd=new ZD(100,100);
        zd.setDirect(2);//向下
        zd.setSpeed(5);
    }
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.fillRect(0,0,1080,680);
        g.setColor(Color.green);
        //花子弹
        g.fillOval(zd.getX(),zd.getY(),10,10);
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode()==KeyEvent.VK_SPACE){
            //子弹发射

            //q:子弹如何自己行动?子弹方向问题?子弹越界问题,子弹如何重复发射?
            while(true){
            zd.move();
            repaint();
            }
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
}

class ZD {
    private int x;
    private int y;
    private int direct;//0上,1右,2下,3左
    private int speed;//速度
    //移动
    public void move(){
            if (direct == 0) {//方向向上
                y -= speed;
            } else if (direct == 1) {
                x += speed;
            } else if (direct == 2) {
                y += speed;
            } else if (direct == 3) {
                x -= speed;
            }
    }

    public ZD(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getDirect() {
        return direct;
    }

    public void setDirect(int direct) {
        this.direct = direct;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }
}